<template>
    <el-dialog v-if="dialogVisible" :append-to-body="true" :close-on-click-modal='false' title="添加广告账户" :visible.sync="dialogVisible" @close="resetForm()" width="600px">
        <el-input placeholder="请输入广告账户名称、广告账户id进行搜索" clearable prefix-icon="el-icon-search" @change="searchData" v-model="queryInfo.query">
        </el-input>
        <div v-loading="loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0)">
            <el-table :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)" class="com_table" height="320px" :row-key="(row)=>{ return row.advertiser_id}" style="margin-top:20px" ref="planRef" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" :reserve-selection="true">
                </el-table-column>
                <el-table-column prop="advertiser_id" label="广告账户 ID" width="200">
                </el-table-column>
                <el-table-column prop="advertiser_name" label="广告账户名称">
                </el-table-column>
            </el-table>
        </div>
        <el-pagination style="margin-top:20px" background layout="prev, pager, next" @current-change="handleCurrentChange" :total="tableData.length">
        </el-pagination>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm()" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm()" class="el-button-width">保 存</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            pagesize:10,
            currentPage:1,
            type:null,
            hasCheck: [],
            planData: [],
            queryInfo: {
                query: "",
            },
            tableData: [],
            total: 0,
        };
    },
    created() {
        
    },
    beforeDestroy() {
        
    },
    methods: {
        //获取广告组
        async getDataList(page) {
            this.queryInfo.page = page;
            this.loading = true
            const { data: res } = await this.$http.post(
                "autodrop/TiktokAdvertiser/advertiserList",
                this.queryInfo
            );
            if (res.code !== 200) {
                this.loading = false
                this.$message.error(res.msg);
                return;
            } else {
                this.loading = false
                this.tableData = res.data.list;
                this.planData.forEach((row) => {
                    this.$refs.planRef.toggleRowSelection(row, true)
                })
            }
        },

        searchData(){
            this.currentPage = 1
            this.getDataList()
        },

        // 添加
        submitForm() {
            if(this.hasCheck.length>0){
                this.$emit('getAdverData',this.hasCheck)
                this.resetForm()
            }else{
                this.$message.error('请选择广告组！');
                return
            }
        },

        // 关闭重置表单
        resetForm() {
            this.dialogVisible = false;
            this.queryInfo.query = ""
            this.currentPage = 1
            this.$refs.planRef.clearSelection();
        },

        // 表格选中
        handleSelectionChange(v) {
            this.hasCheck = v;
        },

        // 每页显示多少条数据
        handleSizeChange(val) {
            this.queryInfo.limit = val;
            this.getDataList(this.planData, 1);
        },
        // 当前页切换
        handleCurrentChange(val) {
            this.currentPage = val;
            // this.setpage = val;
            // this.getDataList(this.planData, val);
        },
    },
    components: {},
};
</script>


<style scoped>
</style>
